package ca.jsbr.tinyUI.input 
{
	import ca.jsbr.as2js_lib.display.canvas.Node2D;
	import ca.jsbr.as2js_lib.display.canvas.View;
	import ca.jsbr.as2js_lib.display.canvas.view.ShapeView;
	import ca.jsbr.as2js_lib.event.MouseJSEvent;
	import ca.jsbr.as2js_lib.utils.enum.Cursor;
	import ca.jsbr.as2js_lib.utils.MouseManager;
	import ca.jsbr.tinyUI.general.QuickText;
	/**
	 * ...
	 * @author JSBR [jsricard@lvlstudio.com]
	 */
	public class Button extends Node2D
	{
		
		///@var configs
		private var _radius:int;
		private var _disable:Array;
		private var _border:int;
		private var _data:Object;
		
		private var _bg:ShapeView;
		private var _txt:QuickText;
		private var _label:String;
		private var _freeze:Boolean;
		private var _icon:View;
		
		public function Button(label:String="",icon:View=null) 
		{
			_icon = icon;
			_width = -1;
			_height = -1;
			_label = label;
			_radius=0;
			build();
			
		}
		
		override public function get parent():Node2D 
		{
			return _parent;
		}
		
		override public function set parent(value:Node2D):void 
		{
			_parent = value;
			draw();
		}
		
		
		/**
		 * Change l'état du bouton pour celui spécifé
		 * @param	state
		 */
		public function setSate(state:int):void {
			if (_freeze)
				return;
			_state = state;
			redraw();
		}

		public function get freeze():Boolean {
			return _freeze; 
		}
		

		public function set freeze(value:Boolean):void 
		{
			_freeze = value;
		}
		
		public function get data():Object 
		{
			return _data;
		}
		
		public function set data(value:Object):void 
		{
			_data = value;
		}
		
		override public function redraw(child:Boolean = false):void {
			//super.redraw(child);
			_bg.clear();
			draw();
			
		}
		

		/**
		 * @param	radius	ajuste le rayon des bords ronds [haut gauche, droite, bas gauche, droite]
		 * @return
		 */
		public function setRadius(radius:int):void {
			_radius=radius
		}
		
		
		override public function set style(value:TinyStyle):void 
		{
			super.style = value;
			_radius = style.buttonRadius;
		}
		
		
		private function build():void
		{
			_bg = new ShapeView();
			addChild(_bg);
			
			_txt = new QuickText();
			addChild(_txt);
			mouseChildren = false;
			
			addEventListener(MouseJSEvent.MOUSE_OVER, onRollOver);
			addEventListener(MouseJSEvent.MOUSE_OUT, onRollOut);
			addEventListener(MouseJSEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseJSEvent.MOUSE_UP, onMouseUp);
		}
		
		
		
		private function draw():void {
			if (!_txt)
				return;
			_txt.text = _label;
			//_txt.render();
			//_txt.redraw();
			var w:Number=_width;
			var h:Number=_height;
			if (_width == -1)
				w = _txt.width + 20;
			if (_height == -1)
				h = _txt.height + 5;
			/*_txt.width = w;
			_txt.height = _txt.textHeight+4;
			_txt.y = h / 2 - _txt.realHeight / 2;*/
			
			_bg.clear();
		
			
			//_bg.beginGradientFill([0xFFFFFF, 0xDDDDDD])
			
			_bg.lineStyle(1, "#CCCCCC");
			if(_state==View.OVER)
				_bg.beginGradientFill(["#FFFFFF", "#CCCCFF"]);
			else if(_state==View.DOWN)
				_bg.beginGradientFill(["#CCCCCC", "#FFFFFF"]);
			else
				_bg.beginGradientFill(["#FFFFFF", "#DDDDDD"]);

			if (_radius==0)
				_bg.drawRect(0, 0, w, h);
			else
				_bg.drawRoundRect(0, 0, w, h, _radius, _radius);
			//_txt.width = _width;
			//_txt.height = _height;
			_txt.y = h / 2 - _txt.height / 2 - 1;
			_txt.x = w / 2 - _txt.width / 2;
			_bg.endFill();
			_width = w;
			_height = h;
		}
		
		
		
		private function onMouseDown(e:MouseEvent):void {
			setSate(View.DOWN);
		}
		
		private function onMouseUp(e:MouseEvent):void {
			setSate(View.OVER);
		}
		
		private function onRollOut(e:MouseEvent):void {
			MouseManager.getInstance().cursor = Cursor.AUTO;
			setSate(View.UP);
		}
		
		private function onRollOver(e:MouseEvent):void {
			MouseManager.getInstance().cursor = Cursor.POINTER;
			setSate(View.OVER);
		}
		
	
	}

}